<%-- 
    Document   : NicholasviewVotes
    Created on : Apr 19, 2011, 12:20:37 AM
    Author     : ChuckToh
--%>
<%@page import="java.sql.ResultSet"%>
<%@page import="NicholasValueBeans.VotingList"%>
<%@page import="NicholasValueBeans.Votes"%>
<h3>View Votes</h3>
<%
            VotingList vl = new VotingList();
            Votes v = new Votes();

            ResultSet allVotingLists = vl.getAllVotingLists();
            ResultSet allVotingLists2 = vl.getAllVotingLists();
            int allVotingListsCount = 0;

            while (allVotingLists2.next()) {
                allVotingListsCount++;
            }

            if (allVotingListsCount > 0) {
                while (allVotingLists.next()) {
                    int votingListId = allVotingLists.getInt("id");
                    ResultSet votesByVotingListId = v.getVotesByVotingListId(votingListId);
                    ResultSet votesByVotingListId2 = v.getVotesByVotingListId(votingListId);
                    int votesByVotingListIdCount = 0;

                    String voteTopics = allVotingLists.getString("topics");
                    String[] voteTopicsArray = voteTopics.split(",");

                    int[] voteCounts = new int[voteTopicsArray.length];
                    for (int i = 0; i < voteCounts.length; i++) {
                        voteCounts[i] = 0;
                    }

                    while (votesByVotingListId2.next()) {
                        votesByVotingListIdCount++;
                    }

                    if (votesByVotingListIdCount > 0) {
                        while (votesByVotingListId.next()) {
                            String topicVoted = votesByVotingListId.getString("topicVoted");
                            for (int i = 0; i < voteCounts.length; i++) {
                                if (topicVoted.equals(voteTopicsArray[i])) {
                                    voteCounts[i]++;
                                }
                            }
                        }
                    }

                    out.println("<h4>Voting List ID: " + votingListId + "</h4>");
                    out.println("<table cellspacing='5' cellpadding='5' border='1'>");
                    out.println("<tr>");
                    for (int i = 0; i < voteTopicsArray.length; i++) {
                        out.println("<th align='center' width='200'>" + voteTopicsArray[i] + "</th>");
                    }
                    out.println("<th align='center' width='200'>Total Votes</th>");
                    out.println("</tr>");
                    out.println("<tr>");
                    for (int i = 0; i < voteCounts.length; i++) {
                        out.println("<td align='center'>" + voteCounts[i] + "</td>");
                    }
                    out.println("<td align='center'>" + votesByVotingListIdCount + "</td>");
                    out.println("</tr>");
                    out.println("</table>");
                }
            }
%>
